package 链表;

/**
 * @author 陈艺博
 * @date 2021-02-17 22:35
 * @description https://leetcode-cn.com/problems/linked-list-cycle/
 **/
public class _141_linked_list_cycle_环形链表 {
    /**
     * #快慢指针思想 如果有环 快指针和慢指针相遇。 如果没有环 快指针先指向最后的null节点
     * @param head
     * @return
     */
    public boolean hasCycle(ListNode head) {
        if (head ==null || head.next == null){
            return false;
        }
        ListNode slow = head;
        ListNode fast = head.next;

        while (fast != null && fast.next != null){
            if (slow == fast){
                return true;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        return false;
    }
}
